<?php
/**
 * 用户购买的矿机列表
 */
namespace app\common\model;

use think\Model;
use app\common\model\Member as MemberModel;
use app\common\model\Mining as MiningModel;
use app\common\model\MiningSpecification as MiningSpecification;

class MemberMining extends Model
{
    protected $name = 'member_mining';

    /**
     *  用户矿机列表
     * @param array $where  条件
     * @param $page 页码
     * @param $limit   分页数量
     * @param string $order 排序
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getList($where = [],$page,$limit,$order = 'id desc'){
        $data = self::where($where)->order($order)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();
        foreach ($data['data'] as $k => $v){
            $member = MemberModel::getDetails($v['mid']);
            $data['data'][$k]['username'] = !empty($member) ? $v['mid'].' | '.$member['username'] : $v['mid'];

            $mining = MiningSpecification::getDetails($v['msid']);
            $data['data'][$k]['mining_name'] = !empty($mining) ? $mining['name'] : '';

        }
        return $data;
    }

    /**
     * 查询所有用户矿机
     * @param array $where  查询条件
     * @param string $order 排序
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getListAll($where = [],$order = 'id desc'){
        return self::where($where)->order($order)->select()->toArray();
    }

    /**
     * 统计数量
     * @param array $where  查询条件
     * @param string $name  统计字段
     * @return mixed
     */
    public static function statistics($where=[],$name='pay_total'){
        return self::where($where)->sum($name);
    }

    /**
     * 添加用户以释放
     * @param $mid  用户ID
     * @param $min_id   矿机ID
     * @param $total_bonus  要增加的释放数量
     */
    public static function update_total_bonus($mid,$min_id,$total_bonus){
        self::where('mid',$mid)->where('min_id',$min_id)->inc('total_bonus',$total_bonus)->update();
    }

    /**
     * 用户矿机订单更新
     * @param $id   订单ID
     * @param $data 更新内容
     * @return bool
     */
    public static function upproduce($id,$data){
        self::where('id',$id)->save($data);
        return true;
    }
}